home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / mh-e / mh-e.el.z / mh-e.el
Encoding:
Text File  |  1998-05-21  |  52.6 KB  |  1,543 lines

  1. ;;; mh-e.el --- GNU Emacs interface to the MH mail system
  2.  
  3. ;;; Copyright (C) 1985,86,87,88,90,92,93,94,95 Free Software Foundation, Inc.
  4.  
  5. (eval '(run-hooks 'mh-e-load-hook))
  6.  
  7. (defconst mh-e-time-stamp "Time-stamp: <95/10/30 19:14:06 gildea>")
  8. (defconst mh-e-version "5.0.2"
  9.   "Version numbers of this version of mh-e.")
  10.  
  11. ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
  12. ;; Version: 5.0.2
  13. ;; Keywords: mail
  14. ;; Bug-reports: include `M-x mh-version' output in any correspondence
  15.  
  16. ;; This file is part of mh-e, part of GNU Emacs.
  17.  
  18. ;; GNU Emacs is free software; you can redistribute it and/or modify
  19. ;; it under the terms of the GNU General Public License as published by
  20. ;; the Free Software Foundation; either version 2, or (at your option)
  21. ;; any later version.
  22.  
  23. ;; GNU Emacs is distributed in the hope that it will be useful,
  24. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. ;; GNU General Public License for more details.
  27.  
  28. ;; You should have received a copy of the GNU General Public License
  29. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  30. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  
  32. ;;; Commentary:
  33.  
  34. ;;; HOW TO USE:
  35. ;;; M-x mh-rmail to read mail.  Type C-h m there for a list of commands.
  36. ;;; C-u M-x mh-rmail to visit any folder.
  37. ;;; M-x mh-smail to send mail.  From within the mail reader, "m" works, too.
  38.  
  39. ;;; MH (Message Handler) is a powerful mail reader.  The MH newsgroup
  40. ;;; is comp.mail.mh; the mailing list is mh-users@ics.uci.edu (send to
  41. ;;; mh-users-request to be added).  See the monthly Frequently Asked
  42. ;;; Questions posting there for information on getting MH and mh-e.
  43.  
  44. ;;; mh-e is an Emacs interface to the MH mail system.
  45. ;;; The mailing list mh-e@x.org is for discussion of mh-e and
  46. ;;; announcements of new versions.  Send a "subscribe" message to
  47. ;;; mh-e-request@x.org to be added.  Do not report bugs here; mail
  48. ;;; them directly to the author (see top of mh-e.el source).
  49. ;;; Include the output of M-x mh-version in any bug report.
  50.  
  51. ;;; mh-e works with GNU Emacs 18 or 19, and MH 6.
  52.  
  53. ;;; NB.  MH must have been compiled with the MHE compiler flag or several
  54. ;;; features necessary for mh-e will be missing from MH commands, specifically
  55. ;;; the -build switch to repl and forw.
  56.  
  57. ;;; Your .emacs might benefit from these bindings:
  58. ;;; (global-set-key "\C-cr" 'mh-rmail)
  59. ;;; (global-set-key "\C-xm" 'mh-smail)
  60. ;;; (global-set-key "\C-x4m" 'mh-smail-other-window)
  61.  
  62. ;;; Change Log:
  63.  
  64. ;;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
  65. ;;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
  66. ;;; Rewritten for GNU Emacs, James Larus 1985.  larus@ginger.berkeley.edu
  67. ;;; Modified by Stephen Gildea 1988.  gildea@lcs.mit.edu
  68. (defconst mh-e-RCS-id "$Id: mh-e.el,v 3.22.1.4 95/10/31 00:14:39 gildea Exp $")
  69.  
  70. ;;; Code:
  71.  
  72. (provide 'mh-e)
  73. (require 'mh-utils)
  74.  
  75.  
  76. ;;; Hooks:
  77.  
  78. (defgroup mh nil
  79.   "Emacs interface to the MH mail system"
  80.   :group 'mail)
  81.  
  82. (defgroup mh-hook nil
  83.   "Hooks to mh-e mode"
  84.   :prefix "mh-"
  85.   :group 'mh)
  86.  
  87.  
  88. (defcustom mh-folder-mode-hook nil
  89.   "Invoked in MH-Folder mode on a new folder."
  90.   :type 'hook
  91.   :group 'mh-hook)
  92.  
  93. (defcustom mh-inc-folder-hook nil
  94.   "Invoked by \\<mh-folder-mode-map>`\\[mh-inc-folder]' after incorporating mail into a folder."
  95.   :type 'hook
  96.   :group 'mh-hook)
  97.  
  98. (defcustom mh-show-hook nil
  99.   "Invoked after \\<mh-folder-mode-map>`\\[mh-show]' shows a message."
  100.   :type 'hook
  101.   :group 'mh-hook)
  102.  
  103. (defcustom mh-show-mode-hook nil
  104.   "Invoked in MH-Show mode on each message."
  105.   :type 'hook
  106.   :group 'mh-hook)
  107.  
  108. (defcustom mh-delete-msg-hook nil
  109.   "Invoked after marking each message for deletion."
  110.   :type 'hook
  111.   :group 'mh-hook)
  112.  
  113. (defcustom mh-refile-msg-hook nil
  114.   "Invoked after marking each message for refiling."
  115.   :type 'hook
  116.   :group 'mh-hook)
  117.  
  118. (defcustom mh-before-quit-hook nil
  119.   "Invoked by \\<mh-folder-mode-map>`\\[mh-quit]' before quitting mh-e.  See also  mh-quit-hook."
  120.   :type 'hook
  121.   :group 'mh-hook)
  122.  
  123. (defcustom mh-quit-hook nil
  124.   "Invoked after \\<mh-folder-mode-map>`\\[mh-quit]' quits mh-e.  See also  mh-before-quit-hook."
  125.   :type 'hook
  126.   :group 'mh-hook)
  127.  
  128.  
  129.  
  130. ;;; Personal preferences:
  131.  
  132. (defcustom mh-lpr-command-format "lpr -J '%s'"
  133.   "*Format for Unix command that prints a message.
  134. The string should be a Unix command line, with the string '%s' where
  135. the job's name (folder and message number) should appear.  The formatted
  136. message text is piped to this command when you type \\<mh-folder-mode-map>`\\[mh-print-msg]'."
  137.   :type 'string
  138.   :group 'mh)
  139.  
  140. (defcustom mh-scan-prog "scan"
  141.   "*Program to run to generate one-line-per-message listing of a folder.
  142. Normally \"scan\" or a file name linked to scan.  This file is searched
  143. for relative to the mh-progs directory unless it is an absolute pathname.
  144. Automatically becomes buffer-local when set in any fashion."
  145.   :type 'string
  146.   :group 'mh)
  147. (make-variable-buffer-local 'mh-scan-prog)
  148.  
  149. (defcustom mh-inc-prog "inc"
  150.   "*Program to run to incorporate new mail into a folder.
  151. Normally \"inc\".  This file is searched for relative to
  152. the mh-progs directory unless it is an absolute pathname."
  153.   :type 'string
  154.   :group 'mh)
  155.  
  156. (defcustom mh-folder-coding-system 'ctext
  157.   "*Coding-system to decode folder."
  158.   :type 'coding-system
  159.   :group 'mh)
  160. (make-variable-buffer-local 'mh-folder-coding-system)
  161.  
  162. (defcustom mh-print-background nil
  163.   "*Print messages in the background if non-nil.
  164. WARNING: do not delete the messages until printing is finished;
  165. otherwise, your output may be truncated."
  166.   :type 'boolean
  167.   :group 'mh)
  168.  
  169. (defcustom mh-recenter-summary-p nil
  170.   "*Recenter summary window when the show window is toggled off if non-nil."
  171.   :type 'boolean
  172.   :group 'mh)
  173.  
  174. (defcustom mh-do-not-confirm nil
  175.   "*Non-nil means do not prompt for confirmation before some mh-e commands.
  176. Affects non-recoverable commands such as mh-kill-folder and mh-undo-folder."
  177.   :type 'boolean
  178.   :group 'mh)
  179.  
  180. (defcustom mh-store-default-directory nil
  181.   "*Last directory used by \\[mh-store-msg]; default for next store.
  182. A directory name string, or nil to use current directory."
  183.   :type '(choice (const :tag "Current" nil)
  184.          directory)
  185.   :group 'mh)
  186.  
  187. ;;; Parameterize mh-e to work with different scan formats.  The defaults work
  188. ;;; with the standard MH scan listings, in which the first 4 characters on
  189. ;;; the line are the message number, followed by two places for notations.
  190.  
  191. (defvar mh-good-msg-regexp  "^....[^D^]"
  192.   "Regexp specifiying the scan lines that are 'good' messages.")
  193.  
  194. (defvar mh-deleted-msg-regexp "^....D"
  195.   "Regexp matching scan lines of deleted messages.")
  196.  
  197. (defvar mh-refiled-msg-regexp  "^....\\^"
  198.   "Regexp matching scan lines of refiled messages.")
  199.  
  200. (defvar mh-valid-scan-line "^ *[0-9]"
  201.   "Regexp matching scan lines for messages (not error messages).")
  202.  
  203. (defvar mh-cur-scan-msg-regexp "^....\\+"
  204.   "Regexp matching scan line for the cur message.")
  205.  
  206. (defvar mh-note-deleted "D"
  207.   "String whose first character is used to notate deleted messages.")
  208.  
  209. (defvar mh-note-refiled "^"
  210.   "String whose first character is used to notate refiled messages.")
  211.  
  212. (defvar mh-note-cur "+"
  213.   "String whose first character is used to notate the current message.")
  214.  
  215. (defvar mh-partial-folder-mode-line-annotation "select"
  216.   "Annotation when displaying part of a folder.
  217. The string is displayed after the folder's name.  NIL for no annotation.")
  218.  
  219.  
  220. ;;; Internal variables:
  221.  
  222. (defvar mh-last-destination nil)    ;Destination of last refile or write command.
  223.  
  224. (defvar mh-folder-mode-map (make-keymap)
  225.   "Keymap for MH folders.")
  226.  
  227. (defvar mh-delete-list nil)        ;List of msg numbers to delete.
  228.  
  229. (defvar mh-refile-list nil)        ;List of folder names in mh-seq-list.
  230.  
  231. (defvar mh-next-direction 'forward)    ;Direction to move to next message.
  232.  
  233. (defvar mh-narrowed-to-seq nil)        ;Sequence display is narrowed to or nil if not narrowed.
  234.  
  235. (defvar mh-first-msg-num nil)        ;Number of first msg in buffer.
  236.  
  237. (defvar mh-last-msg-num nil)        ;Number of last msg in buffer.
  238.  
  239. (defvar mh-mode-line-annotation nil)    ;Indiction this is not the full folder.
  240.  
  241. ;;; Macros and generic functions:
  242.  
  243. (defun mh-mapc (func list)
  244.   (while list
  245.     (funcall func (car list))
  246.     (setq list (cdr list))))
  247.  
  248.  
  249.  
  250. ;;; Entry points:
  251.  
  252. ;;;###autoload
  253. (defun mh-rmail (&optional arg)
  254.   "Inc(orporate) new mail with MH, or, with arg, scan an MH mail folder.
  255. This function is an entry point to mh-e, the Emacs front end
  256. to the MH mail system."
  257.   (interactive "P")
  258.   (mh-find-path)
  259.   (if arg
  260.       (call-interactively 'mh-visit-folder)
  261.       (mh-inc-folder)))
  262.  
  263.  
  264. ;;; mh-smail and mh-smail-other-window have been moved to the new file
  265. ;;; mh-comp.el, but Emacs 18 still looks for them here, so provide a
  266. ;;; definition here, too, for a while.
  267.  
  268. (defun mh-smail ()
  269.   "Compose and send mail with the MH mail system.
  270. This function is an entry point to mh-e, the Emacs front end
  271. to the MH mail system."
  272.   (interactive)
  273.   (mh-find-path)
  274.   (require 'mh-comp)
  275.   (call-interactively 'mh-send))
  276.  
  277.  
  278. (defun mh-smail-other-window ()
  279.   "Compose and send mail in other window with the MH mail system.
  280. This function is an entry point to mh-e, the Emacs front end
  281. to the MH mail system."
  282.   (interactive)
  283.   (mh-find-path)
  284.   (require 'mh-comp)
  285.   (call-interactively 'mh-send-other-window))
  286.  
  287.  
  288.  
  289. ;;; User executable mh-e commands:
  290.  
  291.  
  292. (defun mh-delete-msg (msg-or-seq)
  293.   "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
  294. Default is the displayed message.  If optional prefix argument is
  295. given then prompt for the message sequence."
  296.   (interactive (list (if current-prefix-arg
  297.              (mh-read-seq-default "Delete" t)
  298.              (mh-get-msg-num t))))
  299.   (mh-delete-msg-no-motion msg-or-seq)
  300.   (mh-next-msg))
  301.  
  302.  
  303. (defun mh-delete-msg-no-motion (msg-or-seq)
  304.   "Mark the specified MESSAGE(s) for subsequent deletion.
  305. Default is the displayed message.  If optional prefix argument is
  306. provided, then prompt for the message sequence."
  307.   (interactive (list (if current-prefix-arg
  308.              (mh-read-seq-default "Delete" t)
  309.              (mh-get-msg-num t))))
  310.   (if (numberp msg-or-seq)
  311.       (mh-delete-a-msg msg-or-seq)
  312.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
  313.  
  314.  
  315. (defun mh-execute-commands ()
  316.   "Process outstanding delete and refile requests."
  317.   (interactive)
  318.   (if mh-narrowed-to-seq (mh-widen))
  319.   (mh-process-commands mh-current-folder)
  320.   (mh-set-scan-mode)
  321.   (mh-goto-cur-msg)            ; after mh-set-scan-mode for efficiency
  322.   (mh-make-folder-mode-line)
  323.   t)                    ; return t for [local-]write-file-hooks
  324.  
  325.  
  326. (defun mh-first-msg ()
  327.   "Move to the first message."
  328.   (interactive)
  329.   (goto-char (point-min))
  330.   (while (and (not (eobp)) (not (looking-at mh-valid-scan-line)))
  331.     (forward-line 1)))
  332.  
  333.  
  334. (defun mh-header-display ()
  335.   "Show the current message with all its headers.
  336. Displays headers that might have been suppressed by setting the
  337. variables `mh-clean-message-header' or `mhl-formfile', or by the fallback
  338. behavior of scrolling uninteresting headers off the top of the window.
  339. Type \"\\[mh-show]\" to show the message normally again."
  340.   (interactive)
  341.   (and (not mh-showing-with-headers)
  342.        (or mhl-formfile mh-clean-message-header)
  343.        (mh-invalidate-show-buffer))
  344.   (let ((mhl-formfile nil)
  345.     (mh-clean-message-header nil))
  346.     (mh-show-msg nil)
  347.     (mh-in-show-buffer (mh-show-buffer)
  348.       (goto-char (point-min))
  349.       (mh-recenter 0))
  350.     (setq mh-showing-with-headers t)))
  351.  
  352.  
  353. (defun mh-inc-folder (&optional maildrop-name)
  354.   "Inc(orporate)s new mail into the Inbox folder.
  355. Optional prefix argument specifies an alternate maildrop from the default.
  356. If the prefix argument is given, incorporates mail into the current
  357. folder, otherwise uses the folder named by `mh-inbox'.
  358. Runs `mh-inc-folder-hook' after incorporating new mail.
  359. Do not call this function from outside mh-e; use \\[mh-rmail] instead."
  360.   (interactive (list (if current-prefix-arg
  361.              (expand-file-name
  362.               (read-file-name "inc mail from file: "
  363.                       mh-user-path)))))
  364.   (let ((config (current-window-configuration)))
  365.     (if (not maildrop-name)
  366.     (cond ((not (get-buffer mh-inbox))
  367.            (mh-make-folder mh-inbox)
  368.            (setq mh-previous-window-config config))
  369.           ((not (eq (current-buffer) (get-buffer mh-inbox)))
  370.            (switch-to-buffer mh-inbox)
  371.            (setq mh-previous-window-config config)))))
  372.   (mh-get-new-mail maildrop-name)
  373.   (run-hooks 'mh-inc-folder-hook))
  374.  
  375.  
  376. (defun mh-last-msg ()
  377.   "Move to the last message."
  378.   (interactive)
  379.   (goto-char (point-max))
  380.   (while (and (not (bobp)) (looking-at "^$"))
  381.     (forward-line -1)))
  382.  
  383.  
  384. (defun mh-next-undeleted-msg (&optional arg)
  385.   "Move to the NTH next undeleted message in window."
  386.   (interactive "p")
  387.   (setq mh-next-direction 'forward)
  388.   (forward-line 1)
  389.   (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
  390.      (beginning-of-line)
  391.      (mh-maybe-show))
  392.     (t
  393.      (forward-line -1)
  394.      (if (get-buffer mh-show-buffer)
  395.          (delete-windows-on mh-show-buffer)))))
  396.  
  397.  
  398. (defun mh-refile-msg (msg-or-seq folder)
  399.   "Refile MESSAGE(s) (default: displayed message) into FOLDER.
  400. If optional prefix argument provided, then prompt for message sequence."
  401.   (interactive
  402.    (list (if current-prefix-arg
  403.          (mh-read-seq-default "Refile" t)
  404.        (mh-get-msg-num t))
  405.      (intern
  406.       (mh-prompt-for-folder
  407.        "Destination"
  408.        (or (and mh-default-folder-for-message-function
  409.             (let ((refile-file (mh-msg-filename (mh-get-msg-num t))))
  410.               (save-excursion
  411.             (set-buffer (get-buffer-create mh-temp-buffer))
  412.             (erase-buffer)
  413.             (insert-file-contents refile-file)
  414.             (let ((buffer-file-name refile-file))
  415.               (funcall mh-default-folder-for-message-function)))))
  416.            (and (eq 'refile (car mh-last-destination))
  417.             (symbol-name (cdr mh-last-destination)))
  418.            "")
  419.        t))))
  420.   (setq mh-last-destination (cons 'refile folder))
  421.   (if (numberp msg-or-seq)
  422.       (mh-refile-a-msg msg-or-seq folder)
  423.       (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq folder))
  424.   (mh-next-msg))
  425.  
  426.  
  427. (defun mh-refile-or-write-again (message)
  428.   "Re-execute the last refile or write command on the given MESSAGE.
  429. Default is the displayed message.  Use the same folder or file as the
  430. previous refile or write command."
  431.   (interactive (list (mh-get-msg-num t)))
  432.   (if (null mh-last-destination)
  433.       (error "No previous refile or write"))
  434.   (cond ((eq (car mh-last-destination) 'refile)
  435.      (mh-refile-a-msg message (cdr mh-last-destination))
  436.      (message "Destination folder: %s" (cdr mh-last-destination)))
  437.     (t
  438.      (apply 'mh-write-msg-to-file message (cdr mh-last-destination))
  439.      (message "Destination: %s" (cdr mh-last-destination))))
  440.   (mh-next-msg))
  441.  
  442.  
  443. (defun mh-quit ()
  444.   "Quit the current mh-e folder.
  445. Start by running mh-before-quit-hook.  Restore the previous window
  446. configuration, if one exists.  Finish by running mh-quit-hook."
  447.   (interactive)
  448.   (run-hooks 'mh-before-quit-hook) 
  449.   (mh-update-sequences)
  450.   (mh-invalidate-show-buffer)
  451.   (bury-buffer (current-buffer))
  452.   (if (get-buffer mh-show-buffer)
  453.       (bury-buffer mh-show-buffer))
  454.   (if mh-previous-window-config
  455.       (set-window-configuration mh-previous-window-config))
  456.   (run-hooks 'mh-quit-hook))
  457.  
  458. (defun mh-page-msg (&optional arg)
  459.   "Page the displayed message forwards.
  460. Scrolls ARG lines or a full screen if no argument is supplied."
  461.   (interactive "P")
  462.   (scroll-other-window arg))
  463.  
  464.  
  465. (defun mh-previous-page (&optional arg)
  466.   "Page the displayed message backwards.
  467. Scrolls ARG lines or a full screen if no argument is supplied."
  468.   (interactive "P")
  469.   (mh-in-show-buffer (mh-show-buffer)
  470.     (scroll-down arg)))
  471.  
  472.  
  473. (defun mh-previous-undeleted-msg (&optional arg)
  474.   "Move to the NTH previous undeleted message in window."
  475.   (interactive "p")
  476.   (setq mh-next-direction 'backward)
  477.   (beginning-of-line)
  478.   (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
  479.      (mh-maybe-show))
  480.     (t
  481.      (if (get-buffer mh-show-buffer)
  482.          (delete-windows-on mh-show-buffer)))))
  483.  
  484.  
  485. (defun mh-rescan-folder (&optional range)
  486.   "Rescan a folder after optionally processing the outstanding commands.
  487. If optional prefix argument is provided, prompt for the range of
  488. messages to display.  Otherwise show the entire folder."
  489.   (interactive (list (if current-prefix-arg
  490.              (mh-read-msg-range "Range to scan [all]? ")
  491.                nil)))
  492.   (setq mh-next-direction 'forward)
  493.   (mh-scan-folder mh-current-folder (or range "all")))
  494.  
  495.  
  496. (defun mh-write-msg-to-file (msg file no-headers)
  497.   "Append MESSAGE to the end of a FILE.
  498. If NO-HEADERS (prefix argument) is provided, write only the message body.
  499. Otherwise send the entire message including the headers."
  500.   (interactive
  501.    (list (mh-get-msg-num t)
  502.      (let ((default-dir (if (eq 'write (car mh-last-destination))
  503.                 (file-name-directory (car (cdr mh-last-destination)))
  504.                   default-directory)))
  505.        (read-file-name (format "Save message%s in file: "
  506.                    (if current-prefix-arg " body" ""))
  507.                default-dir
  508.                (if (eq 'write (car mh-last-destination))
  509.                    (car (cdr mh-last-destination))
  510.                  (expand-file-name "mail.out" default-dir))))
  511.      current-prefix-arg))
  512.   (let ((msg-file-to-output (mh-msg-filename msg))
  513.     (output-file (mh-expand-file-name file)))
  514.     (setq mh-last-destination (list 'write file (if no-headers 'no-headers)))
  515.     (save-excursion
  516.       (set-buffer (get-buffer-create mh-temp-buffer))
  517.       (erase-buffer)
  518.       (insert-file-contents msg-file-to-output)
  519.       (goto-char (point-min))
  520.       (if no-headers (search-forward "\n\n"))
  521.       (append-to-file (point) (point-max) output-file))))
  522.  
  523.  
  524. (defun mh-toggle-showing ()
  525.   "Toggle the scanning mode/showing mode of displaying messages."
  526.   (interactive)
  527.   (if mh-showing
  528.       (mh-set-scan-mode)
  529.       (mh-show)))
  530.  
  531.  
  532. (defun mh-undo (msg-or-seq)
  533.   "Undo the pending deletion or refile of the specified MESSAGE(s).
  534. Default is the displayed message.  If optional prefix argument is
  535. provided, then prompt for the message sequence."
  536.   (interactive (list (if current-prefix-arg
  537.              (mh-read-seq-default "Undo" t)
  538.              (mh-get-msg-num t))))
  539.   (cond ((numberp msg-or-seq)
  540.      (let ((original-position (point)))
  541.        (beginning-of-line)
  542.        (while (not (or (looking-at mh-deleted-msg-regexp)
  543.                (looking-at mh-refiled-msg-regexp)
  544.                (and (eq mh-next-direction 'forward) (bobp))
  545.                (and (eq mh-next-direction 'backward)
  546.                 (save-excursion (forward-line) (eobp)))))
  547.          (forward-line (if (eq mh-next-direction 'forward) -1 1)))
  548.        (if (or (looking-at mh-deleted-msg-regexp)
  549.            (looking-at mh-refiled-msg-regexp))
  550.            (progn
  551.          (mh-undo-msg (mh-get-msg-num t))
  552.          (mh-maybe-show))
  553.            (goto-char original-position)
  554.            (error "Nothing to undo"))))
  555.     (t
  556.      (mh-map-to-seq-msgs 'mh-undo-msg msg-or-seq)))
  557.   ;; update the mh-refile-list so mh-outstanding-commands-p will work
  558.   (mh-mapc (function
  559.         (lambda (elt)
  560.           (if (not (mh-seq-to-msgs elt))
  561.           (setq mh-refile-list (delq elt mh-refile-list)))))
  562.        mh-refile-list)
  563.   (if (not (mh-outstanding-commands-p))
  564.       (mh-set-folder-modified-p nil)))
  565.  
  566.  
  567. ;;;###autoload
  568. (defun mh-version ()
  569.   "Display version information about mh-e and the MH mail handling system."
  570.   (interactive)
  571.   (mh-find-progs)
  572.   (set-buffer (get-buffer-create mh-temp-buffer))
  573.   (erase-buffer)
  574.   (insert "  mh-e info:\n\nversion: " mh-e-version "\n" mh-e-time-stamp
  575.       "\nEmacs: " emacs-version " on " (symbol-name system-type) " ")
  576.   (condition-case ()
  577.       (call-process "uname" nil t nil "-a")
  578.     (file-error))
  579.   (insert "\n\n  MH info:\n\n" (expand-file-name "inc" mh-progs) ":\n")
  580.   (let ((help-start (point)))
  581.     (condition-case err-data
  582.     (mh-exec-cmd-output "inc" nil "-help")
  583.       (file-error (insert (mapconcat 'concat (cdr err-data) ": "))))
  584.     (goto-char help-start)
  585.     (search-forward "version: " nil t)
  586.     (beginning-of-line)
  587.     (delete-region help-start (point))
  588.     (goto-char (point-min)))
  589.   (display-buffer mh-temp-buffer))
  590.  
  591.  
  592. (defun mh-visit-folder (folder &optional range)
  593.   "Visit FOLDER and display RANGE of messages.
  594. Do not call this function from outside mh-e; see \\[mh-rmail] instead."
  595.   (interactive (list (mh-prompt-for-folder "Visit" mh-inbox t)
  596.              (mh-read-msg-range "Range [all]? ")))
  597.   (let ((config (current-window-configuration)))
  598.     (mh-scan-folder folder (or range "all"))
  599.     (setq mh-previous-window-config config))
  600.   nil)
  601.  
  602.  
  603. (defun mh-compat-quit ()
  604.   "The \"b\" key is obsolescent; will assume you want \"\\[mh-quit]\" ..."
  605.   ;; Was going to make it run mh-burst-digest, but got complaint that
  606.   ;; 'b' should mean 'back', as it does in info, less, and rn.
  607.   ;; This is a temporary compatibility function.
  608.   (interactive)
  609.   (message "%s" (documentation this-command))
  610.   (sit-for 1)
  611.   (call-interactively 'mh-quit))
  612.  
  613.  
  614. (defun mh-update-sequences ()
  615.   "Update MH's Unseen sequence and current folder and message.
  616. Flush mh-e's state out to MH.  The message at the cursor becomes current."
  617.   (interactive)
  618.   ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
  619.   ;; which updates mh-e's state from MH.
  620.   (let ((folder-set (mh-update-unseen))
  621.     (new-cur (mh-get-msg-num nil)))
  622.     (if new-cur
  623.     (let ((seq-entry (mh-find-seq 'cur)))
  624.       (mh-remove-cur-notation)
  625.       (setcdr seq-entry (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
  626.       (mh-define-sequence 'cur (list new-cur))
  627.       (beginning-of-line)
  628.       (if (looking-at mh-good-msg-regexp)
  629.           (mh-notate nil mh-note-cur mh-cmd-note)))
  630.       (or folder-set
  631.       (save-excursion
  632.         (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast"))))))
  633.  
  634.  
  635.  
  636.  
  637. ;;; Support routines.
  638.  
  639. (defun mh-delete-a-msg (msg)
  640.   ;; Delete the MESSAGE.
  641.   (save-excursion
  642.     (mh-goto-msg msg nil t)
  643.     (if (looking-at mh-refiled-msg-regexp)
  644.     (error "Message %d is refiled.  Undo refile before deleting." msg))
  645.     (if (looking-at mh-deleted-msg-regexp)
  646.     nil
  647.     (mh-set-folder-modified-p t)
  648.     (setq mh-delete-list (cons msg mh-delete-list))
  649.     (mh-add-msgs-to-seq msg 'deleted t)
  650.     (mh-notate msg mh-note-deleted mh-cmd-note)
  651.     (run-hooks 'mh-delete-msg-hook))))
  652.  
  653. (defun mh-refile-a-msg (msg destination)
  654.   ;; Refile MESSAGE in FOLDER.  FOLDER is a symbol, not a string.
  655.   (save-excursion
  656.     (mh-goto-msg msg nil t)
  657.     (cond ((looking-at mh-deleted-msg-regexp)
  658.        (error "Message %d is deleted.  Undo delete before moving." msg))
  659.       ((looking-at mh-refiled-msg-regexp)
  660.        (if (y-or-n-p
  661.         (format "Message %d already refiled.  Copy to %s as well? "
  662.             msg destination))
  663.            (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
  664.                 "-src" mh-current-folder
  665.                 (symbol-name destination))
  666.            (message "Message not copied.")))
  667.       (t
  668.        (mh-set-folder-modified-p t)
  669.        (if (not (memq destination mh-refile-list))
  670.            (setq mh-refile-list (cons destination mh-refile-list)))
  671.        (if (not (memq msg (mh-seq-to-msgs destination)))
  672.            (mh-add-msgs-to-seq msg destination t))
  673.        (mh-notate msg mh-note-refiled mh-cmd-note)
  674.        (run-hooks 'mh-refile-msg-hook)))))
  675.  
  676.  
  677. (defun mh-next-msg ()
  678.   ;; Move backward or forward to the next undeleted message in the buffer.
  679.   (if (eq mh-next-direction 'forward)
  680.       (mh-next-undeleted-msg 1)
  681.       (mh-previous-undeleted-msg 1)))
  682.  
  683.  
  684. (defun mh-set-scan-mode ()
  685.   ;; Display the scan listing buffer, but do not show a message.
  686.   (if (get-buffer mh-show-buffer)
  687.       (delete-windows-on mh-show-buffer))
  688.   (setq mh-showing nil)
  689.   (set-buffer-modified-p (buffer-modified-p)) ;force mode line update
  690.   (if mh-recenter-summary-p
  691.       (mh-recenter nil)))
  692.  
  693.  
  694. (defun mh-undo-msg (msg)
  695.   ;; Undo the deletion or refile of one MESSAGE.
  696.   (cond ((memq msg mh-delete-list)
  697.      (setq mh-delete-list (delq msg mh-delete-list))
  698.      (mh-delete-msg-from-seq msg 'deleted t))
  699.     (t
  700.      (mh-mapc (function (lambda (dest)
  701.                   (mh-delete-msg-from-seq msg dest t)))
  702.           mh-refile-list)))
  703.   (mh-notate msg ?  mh-cmd-note))
  704.  
  705.  
  706.  
  707.  
  708. ;;; The folder data abstraction.
  709.  
  710. (defun mh-make-folder (name)
  711.   ;; Create and initialize a new mail folder called NAME and make it the
  712.   ;; current folder.
  713.   (switch-to-buffer name)
  714.   (setq buffer-read-only nil)
  715.   (erase-buffer)
  716.   (setq buffer-read-only t)
  717.   (mh-folder-mode)
  718.   (mh-set-folder-modified-p nil)
  719.   (setq buffer-file-name mh-folder-filename)
  720.   (mh-make-folder-mode-line))
  721.  
  722.  
  723. ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
  724. (put 'mh-folder-mode 'mode-class 'special)
  725.  
  726. (defun mh-folder-mode ()
  727.   "Major mh-e mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
  728. You can show the message the cursor is pointing to, and step through the
  729. messages.  Messages can be marked for deletion or refiling into another
  730. folder; these commands are executed all at once with a separate command.
  731.  
  732. A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
  733. applies the action to a message sequence.
  734.  
  735. Here is a list of the standard keys for mh-e commands, grouped by function.
  736. This list is purposefully not customized; mh-e has a long history, and many
  737. alternate key bindings as a result.  This list is to encourage users to use
  738. standard keys so the other keys can perhaps someday be put to new uses.
  739.  
  740. t    toggle show or scan-only mode
  741. RET    show message, or back to top if already showing
  742.  
  743. SPC    page message forward
  744. DEL    page message back
  745.  
  746. n    next message
  747. p    previous message
  748. g    go to message by number
  749.  
  750. d    mark for deletion
  751. o, ^    mark for output (refile) to another folder
  752. ?    show folder of pending refile
  753. u    undo delete or refile marking
  754.  
  755. x    execute marked deletes and refiles
  756. i    incorporate new mail
  757.  
  758. m    mail a new message
  759. r    reply to a message
  760. f    forward a message
  761.  
  762. q    quit mh-e
  763.  
  764. M-f    visit new folder
  765. M-r    rescan this folder
  766.  
  767. Here are all the commands with their current binding, listed in key order:
  768. \\{mh-folder-mode-map}
  769.  
  770. Variables controlling mh-e operation are (defaults in parentheses):
  771.  
  772.  mh-recursive-folders (nil)
  773.     Non-nil means commands which operate on folders do so recursively.
  774.  
  775.  mh-bury-show-buffer (t)
  776.     Non-nil means that the buffer used to display message is buried.
  777.     It will never be offered as the default other buffer.
  778.  
  779.  mh-clean-message-header (nil)
  780.     Non-nil means remove header lines matching the regular expression
  781.     specified in mh-invisible-headers from messages.
  782.  
  783.  mh-visible-headers (nil)
  784.     If non-nil, it contains a regexp specifying the headers that are shown in
  785.     a message if mh-clean-message-header is non-nil.  Setting this variable
  786.     overrides mh-invisible-headers.
  787.  
  788.  mh-do-not-confirm (nil)
  789.     Non-nil means do not prompt for confirmation before executing some
  790.     non-recoverable commands such as mh-kill-folder and mh-undo-folder.
  791.  
  792.  mhl-formfile (nil)
  793.     Name of format file to be used by mhl to show messages.
  794.     A value of T means use the default format file.
  795.     Nil means don't use mhl to format messages.
  796.  
  797.  mh-lpr-command-format (\"lpr -p -J '%s'\")
  798.     Format for command used to print a message on a system printer.
  799.  
  800.  mh-scan-prog (\"scan\")
  801.     Program to run to generate one-line-per-message listing of a folder.
  802.     Normally \"scan\" or a file name linked to scan.  This file is searched
  803.     for relative to the mh-progs directory unless it is an absolute pathname.
  804.     Automatically becomes buffer-local when set in any fashion.
  805.  
  806.  mh-print-background (nil)
  807.     Print messages in the background if non-nil.
  808.     WARNING: do not delete the messages until printing is finished;
  809.     otherwise, your output may be truncated.
  810.  
  811.  mh-recenter-summary-p (nil)
  812.     If non-nil, then the scan listing is recentered when the window displaying
  813.     a messages is toggled off.
  814.  
  815.  mh-summary-height (4)
  816.     Number of lines in the summary window including the mode line.
  817.  
  818. The value of mh-folder-mode-hook is called when a new folder is set up."
  819.  
  820.   (kill-all-local-variables)
  821.   (use-local-map mh-folder-mode-map)
  822.   (setq major-mode 'mh-folder-mode)
  823.   (mh-set-mode-name "MH-Folder")
  824.   (mh-make-local-vars
  825.    'mh-current-folder (buffer-name)    ; Name of folder, a string
  826.    'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
  827.    'mh-folder-filename            ; e.g. "/usr/foobar/Mail/inbox/"
  828.    (file-name-as-directory (mh-expand-file-name (buffer-name)))
  829.    'mh-showing nil            ; Show message also?
  830.    'mh-delete-list nil            ; List of msgs nums to delete
  831.    'mh-refile-list nil            ; List of folder names in mh-seq-list
  832.    'mh-seq-list nil            ; Alist of (seq . msgs) nums
  833.    'mh-seen-list nil            ; List of displayed messages
  834.    'mh-next-direction 'forward        ; Direction to move to next message
  835.    'mh-narrowed-to-seq nil        ; Sequence display is narrowed to
  836.    'mh-first-msg-num nil        ; Number of first msg in buffer
  837.    'mh-last-msg-num nil            ; Number of last msg in buffer
  838.    'mh-msg-count nil            ; Number of msgs in buffer
  839.    'mh-mode-line-annotation nil        ; Indiction this is not the full folder
  840.    'mh-previous-window-config nil)    ; Previous window configuration
  841.   (setq truncate-lines t)
  842.   (auto-save-mode -1)
  843.   (setq buffer-offer-save t)
  844.   ;; XEmacs change
  845.   (if (featurep 'scrollbar)
  846.       (set-specifier scrollbar-height (cons (current-buffer) 0)))
  847.   (if (boundp 'local-write-file-hooks)
  848.       (setq local-write-file-hooks '(mh-execute-commands)) ;Emacs 19
  849.     (make-local-variable 'write-file-hooks)
  850.     (setq write-file-hooks '(mh-execute-commands))) ;Emacs 18
  851.   (make-local-variable 'revert-buffer-function)
  852.   (setq revert-buffer-function 'mh-undo-folder)
  853.   (or (assq 'mh-showing minor-mode-alist)
  854.       (setq minor-mode-alist
  855.         (cons '(mh-showing " Show") minor-mode-alist)))
  856.   (run-hooks 'mh-folder-mode-hook))
  857.  
  858.  
  859. (defun mh-make-local-vars (&rest pairs)
  860.   ;; Take VARIABLE-VALUE pairs and make local variables initialized to the
  861.   ;; value.
  862.   (while pairs
  863.     (make-variable-buffer-local (car pairs))
  864.     (set (car pairs) (car (cdr pairs)))
  865.     (setq pairs (cdr (cdr pairs)))))
  866.  
  867.  
  868. (defun mh-scan-folder (folder range)
  869.   ;; Scan the FOLDER over the RANGE.  Return in the folder's buffer.
  870.   (cond ((null (get-buffer folder))
  871.      (mh-make-folder folder))
  872.     (t
  873.      (mh-process-or-undo-commands folder)
  874.      (switch-to-buffer folder)))
  875.   (mh-regenerate-headers range)
  876.   (cond ((zerop (buffer-size))
  877.      (if (equal range "all")
  878.          (message "Folder %s is empty" folder)
  879.        (message "No messages in %s, range %s" folder range))
  880.      (sit-for 5)))
  881.   (mh-goto-cur-msg))
  882.  
  883.  
  884. (defun mh-regenerate-headers (range &optional update)
  885.   ;; scan folder over range RANGE.
  886.   ;; If UPDATE, append the scan lines, otherwise replace.
  887.   (let ((folder mh-current-folder)
  888.     scan-start)
  889.     (message "Scanning %s..." folder)
  890.     (with-mh-folder-updating (nil)
  891.       (if update
  892.       (goto-char (point-max))
  893.     (erase-buffer))
  894.       (setq scan-start (point))
  895.       (let ((coding-system-for-read mh-folder-coding-system))
  896.     (mh-exec-cmd-output mh-scan-prog nil
  897.                 "-noclear" "-noheader"
  898.                 "-width" (window-width)
  899.                 folder range))
  900.       (goto-char scan-start)
  901.       (cond ((looking-at "scan: no messages in")
  902.          (keep-lines mh-valid-scan-line)) ; Flush random scan lines
  903.         ((looking-at "scan: "))    ; Keep error messages
  904.         (t
  905.          (keep-lines mh-valid-scan-line))) ; Flush random scan lines
  906.       (setq mh-seq-list (mh-read-folder-sequences folder nil))
  907.       (mh-notate-user-sequences)
  908.       (or update
  909.       (setq mh-mode-line-annotation
  910.         (if (equal range "all")
  911.             nil
  912.           mh-partial-folder-mode-line-annotation)))
  913.       (mh-make-folder-mode-line))
  914.     (message "Scanning %s...done" folder)))
  915.  
  916.  
  917. (defun mh-get-new-mail (maildrop-name)
  918.   ;; Read new mail from a maildrop into the current buffer.
  919.   ;; Return in the current buffer.
  920.   (let ((point-before-inc (point))
  921.     (folder mh-current-folder)
  922.     (new-mail-p nil))
  923.     (with-mh-folder-updating (t)
  924.       (message (if maildrop-name
  925.            (format "inc %s -file %s..." folder maildrop-name)
  926.            (format "inc %s..." folder)))
  927.       (setq mh-next-direction 'forward)
  928.       (goto-char (point-max))
  929.       (let ((start-of-inc (point))
  930.         (coding-system-for-read mh-folder-coding-system))
  931.     (if maildrop-name
  932.         ;; I think MH 5 used "-ms-file" instead of "-file",
  933.         ;; which would make inc'ing from maildrops fail.
  934.         (mh-exec-cmd-output mh-inc-prog nil folder
  935.                 "-file" (expand-file-name maildrop-name)
  936.                 "-width" (window-width)
  937.                 "-truncate")
  938.         (mh-exec-cmd-output mh-inc-prog nil
  939.                 "-width" (window-width)))
  940.     (message
  941.      (if maildrop-name
  942.          (format "inc %s -file %s...done" folder maildrop-name)
  943.          (format "inc %s...done" folder)))
  944.     (goto-char start-of-inc)
  945.     (cond ((save-excursion
  946.          (re-search-forward "^inc: no mail" nil t))
  947.            (message "No new mail%s%s" (if maildrop-name " in " "")
  948.             (if maildrop-name maildrop-name "")))
  949.           ((re-search-forward "^inc:" nil t) ; Error messages
  950.            (error "inc error"))
  951.           (t
  952.            (mh-remove-cur-notation)
  953.            (setq new-mail-p t)))
  954.     (keep-lines mh-valid-scan-line) ; Flush random scan lines
  955.     (setq mh-seq-list (mh-read-folder-sequences folder t))
  956.     (mh-notate-user-sequences)
  957.     (if new-mail-p
  958.         (progn
  959.           (mh-make-folder-mode-line)
  960.           (mh-goto-cur-msg))
  961.         (goto-char point-before-inc))))))
  962.  
  963.  
  964. (defun mh-make-folder-mode-line (&optional ignored)
  965.   ;; Set the fields of the mode line for a folder buffer.
  966.   ;; The optional argument is now obsolete.  It used to be used to pass
  967.   ;; in what is now stored in the buffer-local variable
  968.   ;; mh-mode-line-annotation.
  969.   (save-excursion
  970.     (mh-first-msg)
  971.     (setq mh-first-msg-num (mh-get-msg-num nil))
  972.     (mh-last-msg)
  973.     (setq mh-last-msg-num (mh-get-msg-num nil))
  974.     (setq mh-msg-count (count-lines (point-min) (point-max)))
  975.     (setq mode-line-buffer-identification
  976.       (list (format "{%%b%s} %d msg%s"
  977.             (if mh-mode-line-annotation
  978.                 (format "/%s" mh-mode-line-annotation)
  979.               "")
  980.             mh-msg-count
  981.             (if (zerop mh-msg-count)
  982.                 "s"
  983.               (if (> mh-msg-count 1)
  984.                   (format "s (%d-%d)" mh-first-msg-num
  985.                       mh-last-msg-num)
  986.                 (format " (%d)" mh-first-msg-num))))))))
  987.  
  988.  
  989. (defun mh-unmark-all-headers (remove-all-flags)
  990.   ;; Remove all '+' flags from the headers, and if called with a non-nil
  991.   ;; argument, remove all 'D', '^' and '%' flags too.
  992.   ;; Optimized for speed (i.e., no regular expressions).
  993.   (save-excursion
  994.     (let ((case-fold-search nil)
  995.       (last-line (1- (point-max)))
  996.       char)
  997.       (mh-first-msg)
  998.       (while (<= (point) last-line)
  999.     (forward-char mh-cmd-note)
  1000.     (setq char (following-char))
  1001.     (if (or (and remove-all-flags
  1002.              (or (eql char (aref mh-note-deleted 0))
  1003.              (eql char (aref mh-note-refiled 0))))
  1004.         (eql char (aref mh-note-cur 0)))
  1005.         (progn
  1006.           (delete-char 1)
  1007.           (insert " ")))
  1008.     (if remove-all-flags
  1009.         (progn
  1010.           (forward-char 1)
  1011.           (if (eql (following-char) (aref mh-note-seq 0))
  1012.           (progn
  1013.             (delete-char 1)
  1014.             (insert " ")))))
  1015.     (forward-line)))))
  1016.  
  1017.  
  1018. (defun mh-remove-cur-notation ()
  1019.   ;; Remove old cur notation (cf mh-goto-cur-msg code).
  1020.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  1021.     (save-excursion
  1022.       (and cur-msg
  1023.        (mh-goto-msg cur-msg t t)
  1024.        (looking-at mh-cur-scan-msg-regexp)
  1025.        (mh-notate nil ?  mh-cmd-note)))))
  1026.  
  1027. (defun mh-goto-cur-msg ()
  1028.   ;; Position the cursor at the current message.
  1029.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  1030.     (cond ((and cur-msg
  1031.         (mh-goto-msg cur-msg t t))
  1032.        (mh-notate nil mh-note-cur mh-cmd-note)
  1033.        (mh-recenter 0)
  1034.        (mh-maybe-show cur-msg))
  1035.       (t
  1036.        (mh-last-msg)
  1037.        (message "No current message")))))
  1038.  
  1039.  
  1040. (defun mh-process-or-undo-commands (folder)
  1041.   ;; If FOLDER has outstanding commands, then either process or discard them.
  1042.   ;; Called by functions like mh-sort-folder, so also invalidate show buffer.
  1043.   (set-buffer folder)
  1044.   (if (mh-outstanding-commands-p)
  1045.       (if (or mh-do-not-confirm
  1046.           (y-or-n-p
  1047.         "Process outstanding deletes and refiles (or lose them)? "))
  1048.       (mh-process-commands folder)
  1049.       (mh-undo-folder)))
  1050.   (mh-update-unseen)
  1051.   (mh-invalidate-show-buffer))
  1052.  
  1053.  
  1054. (defun mh-process-commands (folder)
  1055.   ;; Process outstanding commands for the folder FOLDER.
  1056.   (message "Processing deletes and refiles for %s..." folder)
  1057.   (set-buffer folder)
  1058.   (with-mh-folder-updating (nil)
  1059.     ;; Update the unseen sequence if it exists
  1060.     (mh-update-unseen)
  1061.  
  1062.     ;; Then refile messages
  1063.     (mh-mapc
  1064.      (function
  1065.       (lambda (dest)
  1066.     (let ((msgs (mh-seq-to-msgs dest)))
  1067.       (cond (msgs
  1068.          (apply 'mh-exec-cmd "refile"
  1069.             "-src" folder (symbol-name dest)
  1070.             (mh-coalesce-msg-list msgs))
  1071.          (mh-delete-scan-msgs msgs))))))
  1072.      mh-refile-list)
  1073.     (setq mh-refile-list nil)
  1074.  
  1075.     ;; Now delete messages
  1076.     (cond (mh-delete-list
  1077.        (apply 'mh-exec-cmd "rmm" folder
  1078.           (mh-coalesce-msg-list mh-delete-list))
  1079.        (mh-delete-scan-msgs mh-delete-list)
  1080.        (setq mh-delete-list nil)))
  1081.  
  1082.     ;; Don't need to remove sequences since delete and refile do so.
  1083.  
  1084.     ;; Mark cur message
  1085.     (if (> (buffer-size) 0)
  1086.     (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
  1087.  
  1088.     (and (buffer-file-name (get-buffer mh-show-buffer))
  1089.      (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
  1090.      ;; If "inc" were to put a new msg in this file,
  1091.      ;; we would not notice, so mark it invalid now.
  1092.      (mh-invalidate-show-buffer))
  1093.  
  1094.     (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
  1095.     (mh-unmark-all-headers t)
  1096.     (mh-notate-user-sequences)
  1097.     (message "Processing deletes and refiles for %s...done" folder)))
  1098.  
  1099.  
  1100. (defun mh-update-unseen ()
  1101.   ;; Flush updates to the Unseen sequence out to MH.
  1102.   ;; Return non-NIL iff set the MH folder.
  1103.   (if mh-seen-list
  1104.       (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
  1105.          (unseen-msgs (mh-seq-msgs unseen-seq)))
  1106.     (if unseen-msgs
  1107.         (progn
  1108.           (mh-undefine-sequence mh-unseen-seq mh-seen-list)
  1109.           (while mh-seen-list
  1110.         (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
  1111.         (setq mh-seen-list (cdr mh-seen-list)))
  1112.           (setcdr unseen-seq unseen-msgs)
  1113.           t)            ;since we set the folder
  1114.       (setq mh-seen-list nil)))))
  1115.  
  1116.  
  1117. (defun mh-delete-scan-msgs (msgs)
  1118.   ;; Delete the scan listing lines for each of the msgs in the LIST.
  1119.   (save-excursion
  1120.     (while msgs
  1121.       (if (mh-goto-msg (car msgs) t t)
  1122.       (mh-delete-line 1))
  1123.       (setq msgs (cdr msgs)))))
  1124.  
  1125.  
  1126. (defun mh-outstanding-commands-p ()
  1127.   ;; Returns non-nil if there are outstanding deletes or refiles.
  1128.   (or mh-delete-list mh-refile-list))
  1129.  
  1130.  
  1131. (defun mh-coalesce-msg-list (messages)
  1132.   ;; Give a list of MESSAGES, return a list of message number ranges.
  1133.   ;; Sort of the opposite of mh-read-msg-list, which expands ranges.
  1134.   ;; Message lists passed to MH programs go through this so
  1135.   ;; command line arguments won't exceed system limits.
  1136.   (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
  1137.     (range-high nil)
  1138.     (prev -1)
  1139.     (ranges nil))
  1140.     (while prev
  1141.       (if range-high
  1142.       (if (or (not (numberp prev))
  1143.           (not (eql (car msgs) (1- prev))))
  1144.           (progn            ;non-sequential, flush old range
  1145.         (if (eql prev range-high)
  1146.             (setq ranges (cons range-high ranges))
  1147.           (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
  1148.         (setq range-high nil))))
  1149.       (or range-high
  1150.       (setq range-high (car msgs))) ;start new or first range
  1151.       (setq prev (car msgs))
  1152.       (setq msgs (cdr msgs)))
  1153.     ranges))
  1154.  
  1155. (defun mh-greaterp (msg1 msg2)
  1156.   ;; Sort two message indicators.  Strings are "smaller" than numbers.
  1157.   ;; Legal values are things like "cur", "last", 1, and 1820.
  1158.   (if (numberp msg1)
  1159.      (if (numberp msg2)
  1160.          (> msg1 msg2)
  1161.        t)
  1162.     (if (numberp msg2)
  1163.     nil
  1164.       (string-lessp msg2 msg1))))
  1165.  
  1166.  
  1167.  
  1168. ;;; Basic sequence handling
  1169.  
  1170. (defun mh-delete-seq-locally (seq)
  1171.   ;; Remove mh-e's record of SEQUENCE.
  1172.   (let ((entry (mh-find-seq seq)))
  1173.     (setq mh-seq-list (delq entry mh-seq-list))))
  1174.  
  1175. (defun mh-read-folder-sequences (folder save-refiles)
  1176.   ;; Read and return the predefined sequences for a FOLDER.
  1177.   ;; If SAVE-REFILES is non-nil, then keep the sequences
  1178.   ;; that note messages to be refiled.
  1179.   (let ((seqs ()))
  1180.     (cond (save-refiles
  1181.         (mh-mapc (function (lambda (seq) ; Save the refiling sequences
  1182.                  (if (mh-folder-name-p (mh-seq-name seq))
  1183.                      (setq seqs (cons seq seqs)))))
  1184.              mh-seq-list)))
  1185.     (save-excursion
  1186.       (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
  1187.       (progn
  1188.         ;; look for name in line of form "cur: 4" or "myseq (private): 23"
  1189.         (while (re-search-forward "^[^: ]+" nil t)
  1190.           (setq seqs (cons (mh-make-seq (intern (buffer-substring
  1191.                              (match-beginning 0)
  1192.                              (match-end 0)))
  1193.                         (mh-read-msg-list))
  1194.                    seqs)))
  1195.         (delete-region (point-min) (point))))) ; avoid race with mh-process-daemon
  1196.     seqs))
  1197.  
  1198. (defun mh-read-msg-list ()
  1199.   ;; Return a list of message numbers from the current point to the end of
  1200.   ;; the line.  Expands ranges into set of individual numbers.
  1201.   (let ((msgs ())
  1202.     (end-of-line (save-excursion (end-of-line) (point)))
  1203.     num)
  1204.     (while (re-search-forward "[0-9]+" end-of-line t)
  1205.       (setq num (string-to-int (buffer-substring (match-beginning 0)
  1206.                          (match-end 0))))
  1207.       (cond ((looking-at "-")        ; Message range
  1208.          (forward-char 1)
  1209.          (re-search-forward "[0-9]+" end-of-line t)
  1210.          (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
  1211.                               (match-end 0)))))
  1212.            (if (< num2 num)
  1213.            (error "Bad message range: %d-%d" num num2))
  1214.            (while (<= num num2)
  1215.          (setq msgs (cons num msgs))
  1216.          (setq num (1+ num)))))
  1217.         ((not (zerop num))        ;"pick" outputs "0" to mean no match
  1218.          (setq msgs (cons num msgs)))))
  1219.     msgs))
  1220.  
  1221. (defun mh-notate-user-sequences ()
  1222.   ;; Mark the scan listing of all messages in user-defined sequences.
  1223.   (let ((seqs mh-seq-list)
  1224.     name)
  1225.     (while seqs
  1226.       (setq name (mh-seq-name (car seqs)))
  1227.       (if (not (mh-internal-seq name))
  1228.       (mh-notate-seq name mh-note-seq (1+ mh-cmd-note)))
  1229.       (setq seqs (cdr seqs)))))
  1230.  
  1231.  
  1232. (defun mh-internal-seq (name)
  1233.   ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
  1234.   (or (memq name '(answered cur deleted forwarded printed))
  1235.       (eq name mh-unseen-seq)
  1236.       (eq name mh-previous-seq)
  1237.       (mh-folder-name-p name)))
  1238.  
  1239.  
  1240. (defun mh-delete-msg-from-seq (message sequence &optional internal-flag)
  1241.   "Delete MESSAGE from SEQUENCE.  MESSAGE defaults to displayed message.
  1242. From Lisp, optional third arg INTERNAL-FLAG non-nil means do not
  1243. inform MH of the change."
  1244.   (interactive (list (mh-get-msg-num t)
  1245.              (mh-read-seq-default "Delete from" t)
  1246.              nil))
  1247.   (let ((entry (mh-find-seq sequence)))
  1248.     (cond (entry
  1249.        (mh-notate-if-in-one-seq message ?  (1+ mh-cmd-note) sequence)
  1250.        (if (not internal-flag)
  1251.            (mh-undefine-sequence sequence (list message)))
  1252.        (setcdr entry (delq message (mh-seq-msgs entry)))))))
  1253.  
  1254.  
  1255. (defun mh-undefine-sequence (seq msgs)
  1256.   ;; Remove from the SEQUENCE the list of MSGS.
  1257.   (mh-exec-cmd "mark" mh-current-folder "-delete"
  1258.            "-sequence" (symbol-name seq)
  1259.            (mh-coalesce-msg-list msgs)))
  1260.  
  1261.  
  1262. (defun mh-define-sequence (seq msgs)
  1263.   ;; Define the SEQUENCE to contain the list of MSGS.
  1264.   ;; Do not mark pseudo-sequences or empty sequences.
  1265.   ;; Signals an error if SEQUENCE is an illegal name.
  1266.   (if (and msgs
  1267.        (not (mh-folder-name-p seq)))
  1268.       (save-excursion
  1269.     (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
  1270.                "-sequence" (symbol-name seq)
  1271.                (mh-coalesce-msg-list msgs)))))
  1272.  
  1273.  
  1274. (defun mh-map-over-seqs (func seq-list)
  1275.   ;; Apply the FUNCTION to each element in the list of SEQUENCES,
  1276.   ;; passing the sequence name and the list of messages as arguments.
  1277.   (while seq-list
  1278.     (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
  1279.     (setq seq-list (cdr seq-list))))
  1280.  
  1281.  
  1282. (defun mh-notate-if-in-one-seq (msg notation offset seq)
  1283.   ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
  1284.   ;; message with the CHARACTER at the given OFFSET from the beginning of the
  1285.   ;; listing line.
  1286.   (let ((in-seqs (mh-seq-containing-msg msg nil)))
  1287.     (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
  1288.     (mh-notate msg notation offset))))
  1289.  
  1290.  
  1291. (defun mh-seq-containing-msg (msg &optional include-internal-p)
  1292.   ;; Return a list of the sequences containing MESSAGE.
  1293.   ;; If INCLUDE-INTERNAL-P non-nil, include mh-e internal sequences in list.
  1294.   (let ((l mh-seq-list)
  1295.     (seqs ()))
  1296.     (while l
  1297.       (and (memq msg (mh-seq-msgs (car l)))
  1298.        (or include-internal-p
  1299.            (not (mh-internal-seq (mh-seq-name (car l)))))
  1300.        (setq seqs (cons (mh-seq-name (car l)) seqs)))
  1301.       (setq l (cdr l)))
  1302.     seqs))
  1303.  
  1304.  
  1305.  
  1306.  
  1307. ;;; User prompting commands.
  1308.  
  1309.  
  1310. (defun mh-read-msg-range (prompt)
  1311.   ;; Read a list of blank-separated items.
  1312.   (let* ((buf (read-string prompt))
  1313.      (buf-size (length buf))
  1314.      (start 0)
  1315.      (input ()))
  1316.     (while (< start buf-size)
  1317.       (let ((next (read-from-string buf start buf-size)))
  1318.     (setq input (cons (car next) input))
  1319.     (setq start (cdr next))))
  1320.     (nreverse input)))
  1321.  
  1322.  
  1323.  
  1324. ;;; Build the folder-mode keymap:
  1325.  
  1326. (suppress-keymap mh-folder-mode-map)
  1327. (define-key mh-folder-mode-map "q" 'mh-quit)
  1328. (define-key mh-folder-mode-map "b" 'mh-compat-quit)
  1329. (define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
  1330. (define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
  1331. (define-key mh-folder-mode-map "|" 'mh-pipe-msg)
  1332. (define-key mh-folder-mode-map "\ea" 'mh-edit-again)
  1333. (define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
  1334. (define-key mh-folder-mode-map "\e#" 'mh-delete-seq)
  1335. (define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
  1336. (define-key mh-folder-mode-map "\C-xw" 'mh-widen)
  1337. (define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
  1338. (define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
  1339. (define-key mh-folder-mode-map "\e " 'mh-page-digest)
  1340. (define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
  1341. (define-key mh-folder-mode-map "\ed" 'mh-redistribute)
  1342. (define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
  1343. (define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
  1344. (define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
  1345. (define-key mh-folder-mode-map "\el" 'mh-list-folders)
  1346. (define-key mh-folder-mode-map "\en" 'mh-store-msg)
  1347. (define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
  1348. (define-key mh-folder-mode-map "\eq" 'mh-list-sequences)
  1349. (define-key mh-folder-mode-map "\es" 'mh-search-folder)
  1350. (define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
  1351. (define-key mh-folder-mode-map "l" 'mh-print-msg)
  1352. (define-key mh-folder-mode-map "t" 'mh-toggle-showing)
  1353. (define-key mh-folder-mode-map "c" 'mh-copy-msg)
  1354. (define-key mh-folder-mode-map "i" 'mh-inc-folder)
  1355. (define-key mh-folder-mode-map "x" 'mh-execute-commands)
  1356. (define-key mh-folder-mode-map "e" 'mh-execute-commands)
  1357. (define-key mh-folder-mode-map "f" 'mh-forward)
  1358. (define-key mh-folder-mode-map "m" 'mh-send)
  1359. (define-key mh-folder-mode-map "s" 'mh-send)
  1360. (define-key mh-folder-mode-map "r" 'mh-reply)
  1361. (define-key mh-folder-mode-map "a" 'mh-reply)
  1362. (define-key mh-folder-mode-map "j" 'mh-goto-msg)
  1363. (define-key mh-folder-mode-map "g" 'mh-goto-msg)
  1364. (define-key mh-folder-mode-map "\e<" 'mh-first-msg)
  1365. (define-key mh-folder-mode-map "\e>" 'mh-last-msg)
  1366. (define-key mh-folder-mode-map "\177" 'mh-previous-page)
  1367. (define-key mh-folder-mode-map " " 'mh-page-msg)
  1368. (define-key mh-folder-mode-map "\r" 'mh-show)
  1369. (define-key mh-folder-mode-map "." 'mh-show)
  1370. (define-key mh-folder-mode-map "," 'mh-header-display)
  1371. (define-key mh-folder-mode-map "u" 'mh-undo)
  1372. (define-key mh-folder-mode-map "d" 'mh-delete-msg)
  1373. (define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
  1374. (define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
  1375. (define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
  1376. (define-key mh-folder-mode-map "o" 'mh-refile-msg)
  1377. (define-key mh-folder-mode-map "^" 'mh-refile-msg)
  1378. (define-key mh-folder-mode-map "\C-o" 'mh-write-msg-to-file)
  1379. (define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
  1380. (define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
  1381.  
  1382. ;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
  1383.  
  1384.  
  1385.  
  1386. ;;;autoload the other mh-e parts
  1387.  
  1388. ;;; mh-comp
  1389.  
  1390. (autoload 'mh-smail "mh-comp"
  1391.   "Compose and send mail with the MH mail system.
  1392. This function is an entry point to mh-e, the Emacs front end
  1393. to the MH mail system.
  1394. See documentation of `\\[mh-send]' for more details on composing mail." t)
  1395.  
  1396. (autoload 'mh-smail-other-window "mh-comp"
  1397.   "Compose and send mail in other window with the MH mail system.
  1398. This function is an entry point to mh-e, the Emacs front end
  1399. to the MH mail system.
  1400. See documentation of `\\[mh-send]' for more details on composing mail." t)
  1401.  
  1402. (autoload 'mh-edit-again "mh-comp"
  1403.   "Clean-up a draft or a message previously sent and make it resendable.
  1404. Default is the current message.
  1405. The variable mh-new-draft-cleaned-headers specifies the headers to remove.
  1406. See also documentation for `\\[mh-send]' function." t)
  1407.  
  1408. (autoload 'mh-extract-rejected-mail "mh-comp"
  1409.   "Extract a letter returned by the mail system and make it resendable.
  1410. Default is the current message.  The variable mh-new-draft-cleaned-headers
  1411. gives the headers to clean out of the original message.
  1412. See also documentation for `\\[mh-send]' function." t)
  1413.  
  1414. (autoload 'mh-forward "mh-comp"
  1415.   "Forward a message or message sequence.  Defaults to displayed message.
  1416. If optional prefix argument provided, then prompt for the message sequence.
  1417. See also documentation for `\\[mh-send]' function." t)
  1418.  
  1419. (autoload 'mh-redistribute "mh-comp"
  1420.   "Redistribute a letter.
  1421. Depending on how your copy of MH was compiled, you may need to change the
  1422. setting of the variable mh-redist-full-contents.  See its documentation." t)
  1423.  
  1424. (autoload 'mh-reply "mh-comp"
  1425.   "Reply to a MESSAGE (default: displayed message).
  1426. If optional prefix argument INCLUDEP provided, then include the message
  1427. in the reply using filter mhl.reply in your MH directory.
  1428. Prompts for type of addresses to reply to:
  1429.    from    sender only,
  1430.    to      sender and primary recipients,
  1431.    cc/all  sender and all recipients.
  1432. If the file named by `mh-repl-formfile' exists, it is used as a skeleton
  1433. for the reply.  See also documentation for `\\[mh-send]' function." t)
  1434.  
  1435. (autoload 'mh-send "mh-comp"
  1436.   "Compose and send a letter.
  1437. The file named by `mh-comp-formfile' will be used as the form.
  1438. Do not call this function from outside mh-e; use \\[mh-smail] instead.
  1439. The letter is composed in mh-letter-mode; see its documentation for more
  1440. details.  If `mh-compose-letter-function' is defined, it is called on the
  1441. draft and passed three arguments: to, subject, and cc." t)
  1442.  
  1443. (autoload 'mh-send-other-window "mh-comp"
  1444.   "Compose and send a letter in another window.
  1445. Do not call this function from outside mh-e;
  1446. use \\[mh-smail-other-window] instead.
  1447. See also documentation for `\\[mh-send]' function." t)
  1448.  
  1449. (autoload 'mh-letter-mode "mh-comp"
  1450.   "Mode for composing letters in mh-e.
  1451. For more details, type \\[describe-mode] while in MH-Letter mode." t)
  1452.  
  1453.  
  1454. ;;; mh-funcs
  1455.  
  1456. (autoload 'mh-burst-digest "mh-funcs"
  1457.   "Burst apart the current message, which should be a digest.
  1458. The message is replaced by its table of contents and the messages from the
  1459. digest are inserted into the folder after that message." t)
  1460.  
  1461. (autoload 'mh-copy-msg "mh-funcs"
  1462.   "Copy to another FOLDER the specified MESSAGE(s) without deleting them.
  1463. Default is the displayed message.  If optional prefix argument is
  1464. provided, then prompt for the message sequence." t)
  1465.  
  1466. (autoload 'mh-kill-folder "mh-funcs"
  1467.   "Remove the current folder." t)
  1468.  
  1469. (autoload 'mh-list-folders "mh-funcs"
  1470.   "List mail folders." t)
  1471.  
  1472. (autoload 'mh-pack-folder "mh-funcs"
  1473.   "Renumber the messages of a folder to be 1..n.
  1474. First, offer to execute any outstanding commands for the current folder.
  1475. If optional prefix argument provided, prompt for the range of messages
  1476. to display after packing.  Otherwise, show the entire folder." t)
  1477.  
  1478. (autoload 'mh-pipe-msg "mh-funcs"
  1479.   "Pipe the current message through the given shell COMMAND.
  1480. If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
  1481. Otherwise just send the message's body without the headers." t)
  1482.  
  1483. (autoload 'mh-page-digest "mh-funcs"
  1484.   "Advance displayed message to next digested message." t)
  1485.  
  1486. (autoload 'mh-page-digest-backwards "mh-funcs"
  1487.   "Back up displayed message to previous digested message." t)
  1488.  
  1489. (autoload 'mh-print-msg "mh-funcs"
  1490.   "Print MESSAGE(s) (default: displayed message) on printer.
  1491. If optional prefix argument provided, then prompt for the message sequence.
  1492. The variable mh-lpr-command-format is used to generate the print command.
  1493. The messages are formatted by mhl.  See the variable mhl-formfile." t)
  1494.  
  1495. (autoload 'mh-sort-folder "mh-funcs"
  1496.   "Sort the messages in the current folder by date.
  1497. Calls the MH program sortm to do the work.
  1498. The arguments in the list  mh-sortm-args  are passed to sortm
  1499. if this function is passed an argument." t)
  1500.  
  1501. (autoload 'mh-undo-folder "mh-funcs"
  1502.   "Undo all commands in current folder." t)
  1503.  
  1504. (autoload 'mh-store-msg "mh-funcs"
  1505.   "Store the file(s) contained in the current message into DIRECTORY.
  1506. The message can contain a shar file or uuencoded file.
  1507. Default directory is the last directory used, or initially the value of
  1508. mh-store-default-directory  or the current directory." t)
  1509.  
  1510. (autoload 'mh-store-buffer "mh-funcs"
  1511.   "Store the file(s) contained in the current buffer into DIRECTORY.
  1512. The buffer can contain a shar file or uuencoded file.
  1513. Default directory is the last directory used, or initially the value of
  1514. `mh-store-default-directory' or the current directory." t)
  1515.  
  1516.  
  1517. ;;; mh-pick
  1518.  
  1519. (autoload 'mh-search-folder "mh-pick"
  1520.   "Search FOLDER for messages matching a pattern.
  1521. Add the messages found to the sequence named `search'." t)
  1522.  
  1523. ;;; mh-seq
  1524.  
  1525. (autoload 'mh-delete-seq "mh-seq"
  1526.   "Delete the SEQUENCE." t)
  1527. (autoload 'mh-list-sequences "mh-seq"
  1528.   "List the sequences defined in FOLDER." t)
  1529. (autoload 'mh-msg-is-in-seq "mh-seq"
  1530.   "Display the sequences that contain MESSAGE (default: displayed message)." t)
  1531. (autoload 'mh-narrow-to-seq "mh-seq"
  1532.   "Restrict display of this folder to just messages in SEQUENCE
  1533. Use \\[mh-widen] to undo this command." t)
  1534. (autoload 'mh-put-msg-in-seq "mh-seq"
  1535.   "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
  1536. If optional prefix argument provided, then prompt for the message sequence." t)
  1537. (autoload 'mh-widen "mh-seq"
  1538.   "Remove restrictions from current folder, thereby showing all messages." t)
  1539. (autoload 'mh-rename-seq "mh-seq"
  1540.   "Rename SEQUENCE to have NEW-NAME." t)
  1541.  
  1542. ;;; mh-e.el ends here
  1543.